namespace ACEMPortal.UserControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using System.Configuration;
	using ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses;
    using ScenarioFramework.Modules.DM.DataModel;
    using ScenarioFramework.Modules.DM.Application;

	/// <summary>
	///		Summary description for DocumentReleaseList.
	/// </summary>
	public partial class DocumentReleaseList : System.Web.UI.UserControl
	{
		public string sIdDocument = string.Empty;
		XMLDocumentInstance document = null;
		private string sDocumentVersion=string.Empty;

		private const string DOWNLOADED_STATUS = "DOWNLOADED";
		private const string ACCESSED_STATUS = "ACCESSED";


		protected void Page_Load(object sender, System.EventArgs e)
		{
			document = (XMLDocumentInstance) Session["DocumentInstance"];
			sDocumentVersion=(string)Session["DocumentVersion"];
            if (document == null)
                return;
			if ( Page.IsPostBack )
			{
				sIdDocument = (string)ViewState["DocumentId"];
			}
			else
			{
				sIdDocument = Request.QueryString["DocumentId"];
				ViewState["DocumentId"] = sIdDocument;

				

				this.RetrieveReleaseList();

			}

			
		}

		private void RetrieveReleaseList()
		{
			try
			{
				DocumentManagerHelper DocHelper = new DocumentManagerHelper();
				XMLLogs xmlLogs = document.XMLDocumentVersions.Get(sDocumentVersion).XMLLogs;

				DataTable dt = new DataTable();
				dt.Columns.Add(new DataColumn("Role", typeof(string)));
				dt.Columns.Add(new DataColumn("User", typeof(string)));
				dt.Columns.Add(new DataColumn("AccessLastDate", typeof(string)));
				dt.Columns.Add(new DataColumn("DownloadDate", typeof(string)));
			
				DataRow dr = null;
				XMLLog xmlLog = null;

				System.Collections.IEnumerator myEnumerator = xmlLogs.GetEnumerator();
				while ( myEnumerator.MoveNext() )
				{
					xmlLog = (XMLLog)myEnumerator.Current;

					dr = dt.NewRow();


					//retrieve Role from DocumentTemplate inside RELEASELIST Grant type.
					
					ScenarioFramework.WAPI.Response wapiResponse = new ScenarioFramework.WAPI.Response();
					ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLRoles xmlRoles = null;
					xmlRoles = new ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLRoles();
					
					String sRoles = ScenarioFramework.WAPI.Role.GetRolesByUser (xmlLog.Owner, ref wapiResponse);

					if ( wapiResponse.iCode == 0 ) 
					{
						try 
						{
							xmlRoles.Parse (sRoles);
							foreach ( ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLRole xmlRole in xmlRoles)
							{
								dr[0]+= xmlRole.Name + ";";
							}
						}
						catch 
						{
							dr[0] = string.Empty;
						}
						
					} 
					else
					{
						dr[0] = String.Empty;
					}
					dr[1] = xmlLog.Owner;

					bool bDownloaded=false;
					bool bAccessed = false;

					if ( xmlLog.Status == DOWNLOADED_STATUS ) 
					{
						bDownloaded = true;
						dr[3] = xmlLog.Date;
					}
					else
					{
						bAccessed = true;
						dr[2] = xmlLog.Date;
					}

					XMLStatus status = null;
					for ( int j=0; j<xmlLog.Count; j++)
					{
						status = (XMLStatus)xmlLog[j];

						if ( bDownloaded && status.Status == ACCESSED_STATUS) 
						{
							dr[2] = status.Date;
						}
						else if ( bAccessed && status.Status == DOWNLOADED_STATUS )
						{
							dr[3] = status.Date;
						}
					}

					dt.Rows.Add(dr);
				}
 
				this.dgReleaseList.DataSource = new DataView(dt);
				dgReleaseList.DataBind();
			}
			catch
			{
				// redirect su pagina di errore Parametri di Activity non presenti
				Response.Redirect( SFPortal.Redirect.GoError("NON E' POSSIBILE LEGGERE I DATI XML"));
			}
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion
	}
}
